package com.example.seckill.controller;


import com.alibaba.fastjson.JSONObject;
import com.example.seckill.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Random;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Ding
 * @since 2022-04-25
 */
@Controller
@RequestMapping("/")
public class DemoController {
    @Resource
    private IProductService service;

    /**
     * 获取秒杀页面
     *
     * @param model model
     * @return 秒杀页面
     */
    @RequestMapping(value = {"index", ""}, method = RequestMethod.GET)
    public String seckillpage(Model model) {
        return "seckill";
    }

    /**
     * 获取秒杀商品列表
     *
     * @return json
     */
    @ResponseBody
    @GetMapping("product/getSecKillList")
    public JSONObject getSecKillList() {
        JSONObject json = new JSONObject();
        json.put("list", service.getSeckillList());
        return json;
    }

    /**
     * 后台新增秒杀商品（不开放秒杀）
     *
     * @param request  request
     * @param response response
     * @return json
     */
    @ResponseBody
    @PostMapping("product/toAddItem")
    public Object toAddItem(HttpServletRequest request, HttpServletResponse response) {
        String productId = request.getParameter("productId");
        return service.toAddItem(productId);
    }

    /**
     * 后台开放秒杀（赋予其活动时间和货物数量）
     *
     * @param request  request
     * @param response response
     * @return json
     */
    @ResponseBody
    @PostMapping("product/beginKill")
    public Object beginKill(HttpServletRequest request, HttpServletResponse response) {
        String productId = request.getParameter("productId");
        String num = request.getParameter("num");
        String time = request.getParameter("time");
        return service.beginKill(productId, Integer.parseInt(num), time);
    }

    /**
     * 后台结束秒杀（下架商品，从redis缓存中删除掉）
     * @param request request
     * @param response response
     * @return json
     */
    @ResponseBody
    @PostMapping("product/endKill")
    public Object endKill(HttpServletRequest request, HttpServletResponse response) {
        String productId = request.getParameter("productId");
        return service.endKill(productId);
    }

    /**
     * 后台执行秒杀（抢购操作）
     * @param request request
     * @param response response
     * @return json
     */
    @RequestMapping(value = {"product/doSeckill"}, method = RequestMethod.POST)
    @ResponseBody
    public Object doSeckill(HttpServletRequest request, HttpServletResponse response) {
        //获取秒杀商品id
        String productId = request.getParameter("productId");
        //获取用户id（可以不传此值，会随机生成用户）
        Integer userId = request.getParameter("userId") == null ? new Random().nextInt(50000) : Integer.parseInt(String.valueOf(request.getParameter("userId")));
        int flag = service.doSecKill(userId, productId);
        return flag;
    }

}
